<?php
/**
 *
 */
use Fox\SalesDistribution\Sales;
use Fox\Html\CheckBox;
use Fox\SalesDistribution\Sales\Partner\Role;
use Fox\Html\DropList;
use Fox\Common\Customer;
use Fox\Generator;
$block = new Fox\Html\Block("bResponsible",Msg(Sales::BUNDLE_NAME,Sales::LABEL_RESPONSIBLE));
$role = Role::CONTACT_PERSON;
$agencyId = Xview::instance()->val("agency");
if(isset($params['agency']))
	$agencyId = $params['agency'];
// Agencies
$finder = new Customer();
$agencies = $finder->find(new Fox\Dao\SqlFragment("type = ?",array(Customer::TYPE_OFFICE)));

$agFragments[] = new Fox\Dao\SqlFragment("type = ?",array(Customer::TYPE_OFFICE));
$agFragments[] = User::current()->translateSelectionLimits(array(User::LIMIT_AGENCY => "customerid"));
$limitedAgencies = $finder->find(Fox\Dao\SqlFragment::combine($agFragments),null,null,null,true);

$block->add(new DropList("agency",true,false,true))
		->onChange("ReloadFromObject('bResponsible','sourceid','?mod=customer&act=partners&sac=droplist&role=$role')");
$block->item('agency')->options($limitedAgencies,
						Generator::useAttribute('customerid'),
						Generator::useAttribute('name1'));

	
// Requestor
$block->add(new DropList(array("inquiry_author","sourceid"),true,false,true));
if(empty($agencyId)) {
	$agencyId = $block->item('agency')->initialValue();
}
if(!empty($agencyId))
{
	try {
		$customer = \Fox\Dao::load(Customer::className(),$agencyId);
		/* @var $customer Customer */
		$fragments[] = new Fox\Dao\SqlFragment("p.role = ? ",array($role));
		$partners = $customer->customerPartners($fragments);
		$block->item("sourceid")->options($partners,
					Fox\Generator::useAttribute('customerid'),
					Fox\Generator::concatenate(Fox\Generator::useAttribute('name1'), Fox\Generator::useAttribute('function')," - ")
					);
	}
	catch (Fox\Object\Exception\NotExist $e)
	{
		
	}
}
if(!empty($agencyId) && !isset($limitedAgencies[$agencyId]))
{
	$block->item('agency')->options($agencies,
						Generator::useAttribute('customerid'),
						Generator::useAttribute('name1'));
	$block->item('agency')->input(false);
	$block->item('sourceid')->input(false);		
}
// Operationnal Assignement
$block->add(new Fox\Html\DropList\CheckBox("op_entity",null,null,null,null,null,Fox\Html\DropList\CheckBox::RMODE_VERTICAL))
				->nbOfItemByBlock(3)
				->options($agencies,Generator::useAttribute('customerid'),
								Generator::useAttribute('name1'));

$block->collapsed(false);
return $block;